package com.example.wacoffee_manage.Service.Impl;

import com.example.wacoffee_manage.DTO.UpdatePasswordRequest;
import com.example.wacoffee_manage.Mapper.EmployeeMapper;
import com.example.wacoffee_manage.Service.employeeService;
import com.example.wacoffee_manage.common.PageResult;
import com.example.wacoffee_manage.pojo.Employee;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class employeeServiceImpl implements employeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public PageResult getAll(Employee employee) {
        PageHelper.startPage(employee.getPage(),employee.getPageSize());
        List<Employee> empList=employeeMapper.getAll(employee);
        Page<Employee> page= (Page<Employee>) empList;
        return new com.example.wacoffee_manage.common.PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Employee employee) {
        employeeMapper.add(employee);
    }

    @Override
    public void delete(int id) {
        employeeMapper.delete(id);
    }

    @Override
    public Employee getById(int id) {
        return employeeMapper.getById(id);
    }

    @Override
    public void update(Employee employee) {
        System.out.println(employee);
        employeeMapper.update(employee);
    }

    @Override
    public void updatePassword(UpdatePasswordRequest updatePasswordRequest) {
       if(employeeMapper.getByUsernameAndPassword(updatePasswordRequest.getUsername(),updatePasswordRequest.getOldPassword())==null)throw new RuntimeException("用户名密码错误");
       Integer id=employeeMapper.getByUsernameAndPassword(updatePasswordRequest.getUsername(),updatePasswordRequest.getOldPassword());
        System.out.println(id);
       employeeMapper.updatePassword(id,updatePasswordRequest.getNewPassword());
    }

}